package com.gmail.blackdog1987.carcare.db;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;

/**
 * 
 * @author 董国超
 *
 */
public abstract class BaseModel {

	/** 表名 */
	protected static String sTable = "";

	/** 创建的SQL语句 */
	protected List<String> mCreateSqlList = new ArrayList<String>();
	protected TreeMap<Integer, List<String>> mUpgradeSqlList = new TreeMap<Integer, List<String>>(new UpgradeSqlVersionComparator());

	/**
	 * 获取创建的SQL语句列表。
	 * 
	 * @return 创建的SQL语句列表。
	 */
	public List<String> getCreateSqlList() {
		return mCreateSqlList;
	}

	/**
	 * 获取升级的SQL语句列表。
	 * 
	 * @param oldVersion 历史数据库版本号。
	 * @return 升级的SQL语句列表。
	 */
	public List<String> getUpgradeSqlList(int oldVersion) {
		List<String> upgradeSqlList = new ArrayList<String>();

		Iterator<Integer> iterator = mUpgradeSqlList.keySet().iterator();
		while (iterator.hasNext()) {
			int ver = iterator.next();
			if (ver > oldVersion) {
				upgradeSqlList.addAll(mUpgradeSqlList.get(ver));
			}
		}

		return upgradeSqlList;
	}

	/**
	 * 升级的SQL语句按版本顺序排列排序。
	 */
	private class UpgradeSqlVersionComparator implements Comparator<Integer> {
		@Override
		public int compare(Integer lhs, Integer rhs) {
			return lhs.compareTo(rhs);
		}
	}
}
